package com.manage.platform.controller;

import com.manage.platform.dto.UserDto;
import com.manage.platform.dto.UserLoginDto;
import com.manage.platform.dto.UserUpdatePasswordDto;
import com.manage.platform.excel.ExcelServiceImpl;
import com.manage.platform.excel.UserExcelServiceImpl;
import com.manage.platform.dto.query.UserPageQuery;
import com.manage.platform.service.impl.OrganizeServiceImpl;
import com.manage.platform.service.impl.PositionServiceImpl;
import com.manage.platform.service.impl.ProfessionalTitleServiceImpl;
import com.manage.platform.service.impl.UserServiceImpl;
import com.manage.platform.utils.R;
import com.manage.platform.utils.RPage;
import com.manage.platform.vo.*;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.springframework.core.io.Resource;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @author lk
 * 用户管理
 */
@AllArgsConstructor
@RestController
@RequestMapping("/user")
@Tag(name = "系统设置-用户管理")
public class UserController {

    private final UserServiceImpl userServiceImpl;
    private final OrganizeServiceImpl organizeServiceImpl;
    private final ProfessionalTitleServiceImpl professionalTitleServiceImpl;
    private final PositionServiceImpl positionServiceImpl;
    private final ExcelServiceImpl excelServiceImpl;

    /**
     * 用户登录
     */
    @PostMapping("/login")
    @Operation(summary = "用户登录")
    public R<String> login(@RequestBody UserLoginDto dto) {
        return R.ok(userServiceImpl.login(dto));
    }

    /**
     * 查询当前用户信息
     */
    @GetMapping("/getUserInfo")
    @Operation(summary = "查询当前用户信息")
    public R<UserDetailsVo> getUserInfo() {
        return R.ok(userServiceImpl.getUserInfo());
    }

    /**
     * 用户重置默认密码
     */
    @PutMapping("/resetPassword/{id}")
    @Operation(summary = "用户重置默认密码")
    public R<Boolean> resetPassword(@Schema(description = "用户ID") @PathVariable Long id) {
        return R.ok(userServiceImpl.resetPassword(id));
    }

    /**
     * 用户重置密码
     */
    @PutMapping("/updatePassword")
    @Operation(summary = "用户修改密码")
    public R<Boolean> updatePassword(@RequestBody UserUpdatePasswordDto dto) {
        return R.ok(userServiceImpl.updatePassword(dto));
    }

    /**
     * 查询用户列表
     */
    @PostMapping("/getUserPage")
    @Operation(summary = "查询用户列表")
    public R<RPage<List<UserVo>>> getUserPage(@RequestBody UserPageQuery query) {
        return R.ok(userServiceImpl.getUserPage(query));
    }

    /**
     * 查询用户详情
     */
    @GetMapping("/getUserDetails/{id}")
    @Operation(summary = "查询用户详情")
    public R<UserVo> getUserDetails(@Schema(description = "用户ID") @PathVariable("id") Long id) {
        return R.ok(userServiceImpl.getUserDetails(id));
    }

    /**
     * 添加用户
     */
    @PostMapping("/saveUser")
    @Operation(summary = "添加用户")
    public R<Boolean> saveUser(@RequestBody UserDto dto) {
        return R.ok(userServiceImpl.saveUser(dto));
    }

    /**
     * 修改用户
     */
    @PutMapping("/updateUser")
    @Operation(summary = "修改用户")
    public R<Boolean> updateUser(@RequestBody UserDto dto) {
        return R.ok(userServiceImpl.updateUser(dto));
    }

    /**
     * 删除用户
     */
    @DeleteMapping("/deleteUser/{id}")
    @Operation(summary = "删除用户")
    public R<Boolean> deleteUser(@Schema(description = "用户ID") @PathVariable("id") Long id) {
        return R.ok(userServiceImpl.deleteUser(id));
    }

    /**
     * 人员列表(筛选项)
     */
    @GetMapping("/getUserList")
    @Operation(summary = "人员列表(筛选项)")
    public R<List<OrganizeMemberPartVo>> getUserList(@Schema(description = "人员姓名")
                                                     @RequestParam(value = "realName", required = false) String realName) {
        return R.ok(userServiceImpl.getUserList(realName));
    }

    /**
     * 部门列表(筛选项)
     */
    @GetMapping("/getDepartmentList")
    @Operation(summary = "部门列表(筛选项)")
    public R<List<CommonVo>> getDepartmentList(@Schema(description = "部门名称")
                                               @RequestParam(value = "departmentName", required = false) String departmentName) {
        return R.ok(organizeServiceImpl.getDepartmentList(departmentName));
    }

    /**
     * 职称列表(筛选项)
     */
    @GetMapping("/getProfessionalList")
    @Operation(summary = "职称列表(筛选项)")
    public R<List<CommonVo>> getProfessionalList(@Schema(description = "职称名称")
                                                 @RequestParam(value = "professionalName", required = false) String professionalName) {
        return R.ok(professionalTitleServiceImpl.getProfessionalList(professionalName));
    }

    /**
     * 岗位列表(筛选项)
     */
    @GetMapping("/getPositionList")
    @Operation(summary = "岗位列表(筛选项)")
    public R<List<CommonVo>> getPositionList(@Schema(description = "岗位名称")
                                             @RequestParam(value = "positionName", required = false) String positionName) {
        return R.ok(positionServiceImpl.getPositionList(positionName));
    }

    /**
     * 用户模板
     */
    @GetMapping("/getUserTemplate")
    @Operation(summary = "下载用户模板")
    public ResponseEntity<Resource> getUserTemplate(){
         return excelServiceImpl.getUserTemplate("user");
    }

    /**
     * 用户模板批量导入
     */
    @PostMapping("/importExcel")
    @Operation(summary = "模板批量导入")
    public R<Boolean> importExcel(@RequestParam("file") MultipartFile file) {
        return R.ok(userServiceImpl.importExcel(file));
    }

}
